package com.gf.sydw.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gf.common.BaseController;
import com.gf.common.EncodingUtil;
import com.gf.common.ResultMessage;
import com.gf.common.UUIDGenerator;
import com.gf.pf.model.Flexigrid;
import com.gf.pf.service.MyBatisService;
import com.gf.sydw.model.Sydw;
import com.gf.sydw.service.SydwService;

/**
 * @description 使用单位管理
 * @date   Mar 12, 2015
 */
@Controller
@RequestMapping("/sydw")
public class SydwController extends BaseController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(SydwController.class);

	@Autowired
	private SydwService sydwService;
	
	@Autowired
	private MyBatisService myBatisService;
	
	/**
	 * 使用单位查询页面
	 * @return
	 */
	@RequestMapping(value="/lSydw",method=RequestMethod.GET)
	public String sydwList(){
		return "sydw/sydwList";
	}
	
	/**
	 * 使用单位查询列表
	 * @return
	 */
	@RequestMapping(value="/dataGrid",method=RequestMethod.POST)
	@ResponseBody
	public Flexigrid sydwListC(Flexigrid flex,Sydw sydw){
		sydw.setSydwQymc(EncodingUtil.getInstance().decode(sydw.getSydwQymc()));
		flex.setObj(sydw);
		flex.setRowStart((flex.getPage() - 1) * flex.getRp());
		flex.setRowEnd(flex.getPage() * flex.getRp());
		List<?> rows = myBatisService.getList("sydw.sydwList", flex);
		int total = myBatisService.count("sydw.total", flex);
		flex.setTotal(total);
		flex.setRows(rows);
		return flex;
	}
	
	/**
	 * 添加企业跳转
	 * @return
	 */
	@RequestMapping(value = "/fSydw", method = RequestMethod.GET)
	public String sydwAddForward(){
		LOGGER.info("进入添加企业页面");
		return "sydw/sydwAdd";
	}
	
	/**
	 * 添加企业信息
	 * @return
	 */
	@RequestMapping(value = "/addSydw", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage sydwAdd(Sydw sydw){
		sydw.setId(UUIDGenerator.getInstance().getUUID32());
		sydw.setSydwRksj(new Date());
		sydw.setSydwZhczsj(new Date());
		Integer result=sydwService.addSydw(sydw);
		if(result>0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("企业保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("企业保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 修改企业页面
	 */
	@RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
	public String toUpdate(String sydwId, Model model){
		LOGGER.info("准备进入企业修改页面"+sydwId);
		Sydw sydw= sydwService.getSydwById(sydwId);
		if(sydw!=null){
			model.addAttribute(sydw);
		}
		return "sydw/sydwUpdate";
	}
	
	/**
	 * 修改企业页面
	 */
	@RequestMapping(value = "/updateSydw", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage sydwUpdate(Sydw sydw){
		LOGGER.info("准备修改企业信息");
		sydw.setSydwZhczsj(new Date());
//		Integer result= sydwService.updatesydw(sydw);
		Integer result= myBatisService.update("sydw.updateSydw", sydw);
	
		if(result>0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("企业保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("企业保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 删除企业信息
	 * @return
	 */
	@RequestMapping(value = "/deleteSydw", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage sydwDelete(String sydwId){
		LOGGER.info("删除企业信息");
		Integer result= sydwService.sydwDelete(sydwId);
		if(result>0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("企业保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("企业保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 使用单位查询页面
	 * @return
	 */
	@RequestMapping(value="/lSelectSydw",method=RequestMethod.GET)
	public String sydwSelectList(){
		LOGGER.info("进入使用单位选择页面");
		return "sydw/sydwSelectList";
	}
	
	/**
	 * 使用单位查询列表
	 * @return
	 */
	@RequestMapping(value="/dataSelectGrid",method=RequestMethod.POST)
	@ResponseBody
	public Flexigrid sydwSelectList(Flexigrid flex,Sydw sydw){
		LOGGER.info("进入使用单位选择列表查询");
		sydw.setSydwQymc(EncodingUtil.getInstance().decode(sydw.getSydwQymc()));
		flex.setObj(sydw);
		flex.setRowStart((flex.getPage() - 1) * flex.getRp());
		flex.setRowEnd(flex.getPage() * flex.getRp());
		List<?> rows = myBatisService.getList("sydw.sydwSelectList", flex);
		int total = myBatisService.count("sydw.totalSelect", flex);
		flex.setTotal(total);
		flex.setRows(rows);
		return flex;
	}
}